#include "CBitmapFont.h"
#include <stdio.h>
#include "tinyxml/tinyxml.h"
#include <string>
using std::string;

void CBitmapFont::Init(char *szFilename)
{
	LoadBitmapText(szFilename);
}

void CBitmapFont::Kill()
{
	//	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nImageID);
}

void CBitmapFont::DrawBitmapText(char* cText, int nPosX, int nPosY, float fScale, DWORD dwColor)
{
	string thestringtoprint = cText;
	float fPrintX = (float)nPosX;
	for(unsigned int i = 0; i < thestringtoprint.size(); ++i)
	{

		if(thestringtoprint[i] == ' ')
		{ 
			fPrintX += (float)m_nSize/2 * fScale;
			continue;
		}  
		else if(thestringtoprint[i] == '\n')
		{
			nPosY += m_vChars['a'-32].Height() * (int)fScale;
			fPrintX = (float)nPosX;
			continue;
		}
		CSGD_TextureManager::GetInstance()->Draw(m_nImageID, 
			(int)fPrintX + m_vChars[thestringtoprint[i]-32].m_nXOffset, 
			nPosY, fScale, fScale, &m_vChars[thestringtoprint[i]-32].m_rectDraw, 
			0, 0, 0, dwColor);
		fPrintX += (m_vChars[thestringtoprint[i]-32].Width() * fScale);
	}
}

bool CBitmapFont::LoadBitmapText(char* szFont)
{
	TiXmlDocument doc;	//	Represents the ENTIRE file


	char buffer[256];
	sprintf_s(	buffer, _countof(buffer), 
		"Resource/Font/%s",
		szFont);

	if(!doc.LoadFile(buffer))
		return false;

	TiXmlElement* pRoot = doc.RootElement();

	//	There is no File O.o
	if(!pRoot)
		return false;

	TiXmlElement* pLevelxml = pRoot->FirstChildElement("Info");

	//	Leave function if it couldn't find it
	if(!pRoot)
		return false;
	//Load the info, Size of font
	TiXmlElement* pInfo = pRoot->FirstChildElement("info");
	pInfo->Attribute("size",&m_nSize);
	//Load the File name
	TiXmlElement* pPages = pInfo->NextSiblingElement("pages");
	TiXmlElement* pPage = pPages->FirstChildElement("page");
	char szBuffer[128];
	const char* pFile = pPage->Attribute("file");
	//Copy the file name
	if(pFile)
		strcpy_s(szBuffer,_countof(szBuffer),pFile);
	//Build the path to the file
	char szPath2[128];
	strcpy_s(szPath2,sizeof(szPath2),"resource/font/");
	szPath2[sizeof(szPath2)-1] = '\0';
	strcat_s(szPath2,sizeof(szPath2),szBuffer);
	szPath2[sizeof(szPath2)-1] = '\0';
	//Use the file name to load the image ID
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(szPath2,D3DCOLOR_ARGB(255,0,0,0));

	//Find the number of chars
	TiXmlElement* pChars = pPages->NextSiblingElement("chars");
	int nNumChars = 0;
	pChars->Attribute("count",&nNumChars);
	//Loop through and read all of the chars in
	TiXmlElement* pChar = pChars->FirstChildElement("char");
	while (pChar)
	{
		tChar chTmp;
		int nLeft = 0;
		int nTop = 0;
		int nWidth = 0;
		int nHeight = 0;
		pChar->Attribute("id",&chTmp.m_nID);
		pChar->Attribute("x",&nLeft);
		pChar->Attribute("y",&nTop);
		pChar->Attribute("width",&nWidth);
		pChar->Attribute("height",&nHeight);
		pChar->Attribute("xoffset",&chTmp.m_nXOffset);
		pChar->Attribute("yoffset",&chTmp.m_nYOffset);
		chTmp.m_rectDraw.left = nLeft;
		chTmp.m_rectDraw.bottom = nTop + nHeight;
		chTmp.m_rectDraw.top = nTop;
		chTmp.m_rectDraw.right = nLeft + nWidth;
		m_vChars.push_back(chTmp);
		pChar = pChar->NextSiblingElement("char");
	}

	return true;
	//m_nImageID = m_pTM->LoadTexture(szPath);
}